package servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import bean.User;
import dao.UserDAO;

@WebServlet(name = "LoginServlet")
public class RegisterServlet extends HttpServlet {
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        User user = new UserDAO().getUser(username);
        System.out.println(username+"+"+password);
        System.out.println(user.getUsername()+"+"+user.getPassword());
        if (user.getUsername() == null){
            user.setUsername(username);
            user.setPassword(password);
            new UserDAO().addUser(user);
            System.out.println("注册成功！");
            resp.sendRedirect("login.jsp");
        }else {
            req.setAttribute("flag","false");//用户已存在
            req.getRequestDispatcher("/register.jsp").forward(req,resp);
        }



    }
}
